package cn.acewill.yxd.yunxundian.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;

/**
 * Created by AAA on 2017/4/7.
 */

public class EvaluationAdapter extends BaseAdapter {

    private Schedule bean;
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private  TaskCallBack taskCallBack;
    private Context context;
    private List<Schedule> list;
    public EvaluationAdapter(Context context,List<Schedule> list,TaskCallBack taskCallBack){
        this.context=context;
        this.list=list;
        this.taskCallBack=taskCallBack;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.evaluation_main_info,parent,false);
            holder=new ViewHolder();
            holder.tableName= (TextView) convertView.findViewById(R.id.tableName);
            holder.tableDate= (TextView) convertView.findViewById(R.id.tableDate);
            holder.score= (TextView) convertView.findViewById(R.id.score);
            holder.shopName= (TextView) convertView.findViewById(R.id.shopName);
            holder.EvaState= (TextView) convertView.findViewById(R.id.EvaState);
            holder.lookAll= (TextView) convertView.findViewById(R.id.lookAll);
            holder.modifiTask= (TextView) convertView.findViewById(R.id.modifiTask);
            holder.deleteTask= (TextView) convertView.findViewById(R.id.deleteTask);
            holder.signCon=(TextView)convertView.findViewById(R.id.signCon);
            holder.commit= (TextView) convertView.findViewById(R.id.commit);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        bean=list.get(position);
        if (bean.getTitle()==null){
            holder.tableName.setText("命名中....");
        }else{
            holder.tableName.setText(bean.getTitle());
        }
        //holder.tableDate.setText(bean.getEvaDate());
        //holder.score.setText(bean.getScore());
        //holder.shopName.setText(bean.getEvaShopName());
        if(bean.getStates()==1){
            holder.EvaState.setText("任务未开始");
            holder.modifiTask.setVisibility(View.VISIBLE);
            holder.deleteTask.setVisibility(View.VISIBLE);
            holder.commit.setVisibility(View.VISIBLE);
            holder.signCon.setVisibility(View.VISIBLE);
            holder.lookAll.setVisibility(View.GONE);
            holder.score.setText("暂无评分");
        }else if (bean.getStates()==2){
            holder.EvaState.setText("任务进行中");
            holder.modifiTask.setVisibility(View.VISIBLE);
            holder.deleteTask.setVisibility(View.VISIBLE);
            holder.commit.setVisibility(View.VISIBLE);
            holder.signCon.setVisibility(View.VISIBLE);
            holder.lookAll.setVisibility(View.GONE);
            holder.score.setText("暂无评分");
        }
        else if (bean.getStates()==3){
            holder.EvaState.setText("已完成");
            holder.modifiTask.setVisibility(View.GONE);
            holder.deleteTask.setVisibility(View.GONE);
            holder.commit.setVisibility(View.GONE);
            holder.signCon.setVisibility(View.GONE);
            holder.lookAll.setVisibility(View.VISIBLE);
            holder.score.setText("分数（80）");
        }
        else if (bean.getStates()==4){
            holder.EvaState.setText("已提交");
            holder.modifiTask.setVisibility(View.GONE);
            holder.deleteTask.setVisibility(View.GONE);
            holder.commit.setVisibility(View.GONE);
            holder.signCon.setVisibility(View.GONE);
            holder.lookAll.setVisibility(View.VISIBLE);
            holder.score.setText("分数（100）");
        }
        if(bean.getSchStartTime()==null){
            holder.tableDate.setText("时间待定");
        }else{
            String[] array=bean.getSchStartTime().split("\\-|T");
            holder.tableDate.setText(array[0]+"年"+array[1]+"月"+array[2]+"日");
        }


        int shopID=bean.getShopID();
        try {
            if(shopID==0){
                holder.shopName.setText("非门店任务");
            }else{
                Shop shop=db.selector(Shop.class).where("ID","=",shopID).findFirst();
                if(shop==null){
                    holder.shopName.setText("命名中....");
                }else{
                    String shopname=shop.getShopName();
                    holder.shopName.setText(shopname);
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        //删除
        holder.deleteTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                taskCallBack.Dclick(position);


            }
        });
        //查看
        holder.lookAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean=list.get(position);
                taskCallBack.LClick(bean);
            }
        });
        //提交
        holder.commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean=list.get(position);
                taskCallBack.CClick(bean);
            }
        });
        //编辑
        holder.modifiTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean=list.get(position);
                taskCallBack.MClick(bean);
            }
        });

        holder.signCon.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                bean=list.get(position);
                taskCallBack.SClick(bean);
            }
        });
        return convertView;
    }



    class ViewHolder{
        TextView tableName;
        TextView tableDate;
        TextView score;
        TextView shopName;
        TextView EvaState;
        TextView lookAll;
        TextView modifiTask;
        TextView deleteTask;
        TextView commit;
        TextView signCon;
    }

    public interface TaskCallBack{
        void CClick(Schedule bean);
        void LClick(Schedule bean);
        void MClick(Schedule bean);
        void Dclick(int position);
        void SClick(Schedule bean);

    }



}
